/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import Business.Customer_BS;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author anhvtn_b00545
 */
public class Customer_DAO extends ConnectionDB{
    Connection conn= null;
    ResultSet rs= null;
    boolean flag= false;

     public static boolean insertCustomer(Business.Customer_BS c){
        String sql = "{call spInsertCustomer(?,?,?,?,?,?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);

            cs.setString(1,c.getCName());
            cs.setInt(2,c.getCType());
            cs.setString(3,c.getCAddress());
            cs.setString(4,c.getCPhone());
            cs.setString(5,c.getCEmail());
            cs.setString(6,c.getCRelationship());

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
            return false;
        }
     }
    
     public static boolean updateCustomer(Business.Customer_BS c){
        String sql = "{call spUpdateCustomer(?,?,?,?,?,?,?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);
            cs.setInt(1, c.getcID());
            cs.setString(2,c.getCName());
            cs.setInt(3,c.getCType());
            cs.setString(4,c.getCAddress());
            cs.setString(5,c.getCPhone());
            cs.setString(6,c.getCEmail());
            cs.setString(7,c.getCRelationship());

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            //JOptionPane.showMessageDialog(null, ex.getMessage());
            return false;
        }
    }
    
    public static boolean delete(int customerId){
        String sql = "{call spDeleteCustomer(?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);

            cs.setInt(1,customerId);

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            return false;
        }
    }

    public List<Customer_BS> getAllCustomer() {
        ArrayList<Customer_BS> list = new ArrayList<Customer_BS>();
        conn = Customer_DAO.getConnection();
        try {
            java.sql.Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("EXEC spGetAllCustomers");
            while(rs.next()) {
                int id = rs.getInt("cID");
                String name = rs.getString("cName");
                list.add(new Customer_BS(id, name));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static ArrayList<Business.Customer_BS> getCustomers(int customerType, String name){
        String sql = "{call spSearchCustomers (?,?)}";
        ArrayList<Business.Customer_BS> clst = new ArrayList<Business.Customer_BS>();

        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);
            cs.setInt(1, customerType);
            cs.setString(2, name);
            
            ResultSet rsCustomer = cs.executeQuery();
            while(rsCustomer.next()){
                Business.Customer_BS c = new Business.Customer_BS();
                c.setcID(rsCustomer.getInt("cID"));
                c.setCName(rsCustomer.getString("CName"));
                c.setCType(rsCustomer.getInt("CType"));
                c.setCAddress(rsCustomer.getString("CAddress"));
                c.setCPhone(rsCustomer.getString("CPhone"));
                c.setCEmail(rsCustomer.getString("CEmail"));
                c.setCRelationship(rsCustomer.getString("CRelationship"));
                clst.add(c);

            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }
            return clst;
    }
}
